package fm.dongman.modellib.source;

import android.support.annotation.NonNull;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import fm.dongman.animefm.contractlib.BaseDataSource;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.contractlib.contract.CommentListContract;
import fm.dongman.modellib.RemoteHelper;
import fm.dongman.modellib.StaticArea.StaticUrl;
import fm.dongman.modellib.remote.RemoteOperationHelper;

/**
 * 评论列表
 * Created by KUIGE on 2017/10/29.
 */
public class CommentListRepository implements CommentListContract.ICommentListDataSource {
    private static final int mSize = 10;     //每一页数量
    private static CommentListRepository INSTANCE;
    private RemoteOperationHelper mRemote;
    private boolean isRemote = true;
    /**
     * 动漫
     */
    public static final String SUBJECT = "subject";
    /**
     * 漫单
     */
    public static final String MANDAN = "mandan";
    /**
     * 文章
     */
    public static final String ARTICLE = "article";
    /**
     * 查看自己的评论列表
     */
    public static final String USER_OWN = "user";
    /**
     * 查看对象的评论列表
     */
    public static final String USER_OTHERS = "object";

    //设置条件
    private int mPage;
    private String mType; // 评论对象类型
    private String mAscriptionType; // 评论类型
    private String mAscriptionId;

    private CommentListRepository() {
        mRemote = RemoteOperationHelper.getOperationHelper();
    }

    public static CommentListRepository getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new CommentListRepository();
        }
        return INSTANCE;
    }

    @Override
    public void setCommentListInfo(int page, @Nullable String type, @NotNull String ascriptionType, @Nullable String ascriptionId) {
        mPage = page;
        mType = type;
        mAscriptionType = ascriptionType;
        mAscriptionId = ascriptionId;
    }

    @Override
    public void supportComment(@NonNull String commentId, @NonNull String userId, @NonNull BaseDataSource.LoadSourceCallback<? super String> callback) {
        if (isRemote) {
            HashMap<String, String> map = new HashMap<>();
            map.put("session_key", StaticUrl.TOKEN_KEY);
            map.put("comment_id", commentId);
            JSONObject object = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.COMMENT_SUPPORT, map);
            if (object == null) {
                callback.onDataLoadFiled();
                return;
            }
            callback.onDataLoaded(null);
        }else callback.onDataLoadFiled();
    }

    @Override
    public void getData(@NonNull BaseDataSource.LoadSourceCallback<? super List<? extends IModel.ICommentModel>> callback) {
        if (isRemote) {
            if (mAscriptionType.equals(USER_OWN) || mAscriptionType.equals(USER_OTHERS)) {
                parseUserInfoComments(callback, mAscriptionType);
                return;
            }
            parseComments(callback, mAscriptionType);
        }else callback.onDataLoadFiled();
    }

    @Override
    public void deleteComment(String s, LoadSourceCallback<? super String> loadSourceCallback) {
        if (isRemote) {
            HashMap<String, String> map = new HashMap<>();
            map.put("session_key", StaticUrl.TOKEN_KEY);
            map.put("comment_id", s);
            JSONObject jsonObject = RemoteHelper.Companion.getInstance().parsePostUrl("http://api.dongman.fm/itct/delete_comment", map);
            if (jsonObject != null) {
                try {
                    if (jsonObject.getInt("code") == 200) {
                        loadSourceCallback.onDataLoaded("");
                    } else {
                        loadSourceCallback.onDataLoadFiled();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else loadSourceCallback.onDataLoadFiled();
        }else loadSourceCallback.onDataLoadFiled();
    }

    /**
     * 解析用户信息相关评论
     *
     * @param callback 回掉接口
     * @param type     类型
     */
    private void parseUserInfoComments(@NonNull LoadSourceCallback<? super List<? extends IModel.ICommentModel>> callback, String type) {
        // 用户的评论没有页数
        if (mPage != 1){
            return;
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("session_key", StaticUrl.TOKEN_KEY);
        // 是否为查看别人的评论
        if (type.equals(USER_OTHERS)) {
            map.put("id", mAscriptionId);
        }
        JSONObject jsonObj = RemoteHelper.Companion.getInstance().parsePostUrl(StaticUrl.USER_COMMENT, map);
        if (jsonObj == null) {
            callback.onDataLoadFiled();
            return;
        }
        callback.onDataLoaded(mRemote.downUser_CommentList(jsonObj));
    }

    /**
     * 解析相关评论列表
     *
     * @param callback 回掉接口
     * @param type     类型
     */
    private void parseComments(@NonNull LoadSourceCallback<? super List<? extends IModel.ICommentModel>> callback, String type) {
        if (isRemote) {
            JSONObject jsonObj = RemoteHelper.Companion.getInstance().parseGetUrl(StaticUrl.COMMENT_lIST + "target_id=" + mAscriptionId + "&target_type=" + type + "&page=" + mPage + "&size=" + mSize);
            if (jsonObj == null) {
                callback.onDataLoadFiled();
                return;
            }
            switch (mAscriptionType) {
                case SUBJECT:
                    callback.onDataLoaded(mRemote.downCommentComicListData(jsonObj));
                    break;
                case MANDAN:
                    callback.onDataLoaded(mRemote.downCommentSheetData(jsonObj));
                    break;
                case ARTICLE:
                    callback.onDataLoaded(mRemote.downCommentArticleData(jsonObj));
                    break;
            }
        }else callback.onDataLoadFiled();
    }

    @Override
    public void saveData(List<? extends IModel.ICommentModel> dataModel) {
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        isRemote = !isRemote;
    }
}
